﻿Imports System
Imports System.Collections.Generic
Imports AutoMapper
Imports VMWorkshop.Domain

Class DemoViewModel
    ' Private fields
    Private _products As IList(Of ProductListView)
    Private _repository As IProductRepository
    Private _isEditFormVisible As Boolean
    Private _isUpdateMessageVisible As Boolean
    Private _selectedProduct As ProductEditView

    ' Public properties
    Public Property IsEditFormVisible() As Boolean
        Get
            Return Me._isEditFormVisible
        End Get
        Set(ByVal value As Boolean)
            Me._isEditFormVisible = value
        End Set
    End Property
    Public Property IsUpdateMessageVisible() As Boolean
        Get
            Return Me._isUpdateMessageVisible
        End Get
        Set(ByVal value As Boolean)
            Me._isUpdateMessageVisible = value
        End Set
    End Property

    Public Property Products() As IList(Of ProductListView)
        Get
            If Me._products Is Nothing Then
                Me._products = Me.GetProducts()
            End If
            Return Me._products
        End Get
        Set(ByVal value As IList(Of ProductListView))
            Me._products = value
        End Set
    End Property

    Public Property SelectedProduct() As ProductEditView
        Get
            Return Me._selectedProduct
        End Get
        Set(ByVal value As ProductEditView)
            Me._selectedProduct = value
        End Set
    End Property

    ' Constructor
    Public Sub New(ByVal repository As IProductRepository)
        Me._repository = repository
        Me.ClearSelectedProduct()
    End Sub

    ' Public API methods
    Public Sub Persist()
        Dim newProduct As Product = ProductEditView.ToProduct(Me.SelectedProduct)
        Dim oldProduct As Product = Me._repository.GetProductById(newProduct.ProductId)

        With oldProduct
            .Title = newProduct.Title
            .Description = newProduct.Description
            .QuantityOnHand = newProduct.QuantityOnHand
            .ReleaseDate = newProduct.ReleaseDate
            .Price = newProduct.Price
        End With

        Me._repository.Persist(oldProduct)

        Me.IsEditFormVisible = False
        Me.IsUpdateMessageVisible = True
    End Sub

    Public Sub GetSelectedProduct(ByVal productId As String)
        Dim num As Integer

        If Integer.TryParse(productId, num) Then
            Me.GetSelectedProduct(num)
        Else
            Throw New ApplicationException("Invalid integer value for ProductId")
        End If
    End Sub

    Public Sub GetSelectedProduct(ByVal productId As Integer)
        Dim product As Product = Me._repository.GetProductById(productId)
        Me.SelectedProduct = ProductEditView.ToProductEditView(product)

        Me.IsEditFormVisible = True
        Me.IsUpdateMessageVisible = False
    End Sub

    ' Utilities
    Private Function GetProducts() As IList(Of ProductListView)
        Dim products As IList(Of Product) = Me._repository.GetAll()
        Return Mapper.Map(Of IList(Of Product), IList(Of ProductListView))(products)
    End Function

    Private Sub ClearSelectedProduct()
        Me.SelectedProduct = New ProductEditView()
    End Sub
End Class